var ajax_loader = document.createElement('iframe');
ajax_loader.id = 'ajax_loader';
ajax_loader.id = 'ajax_loader';
ajax_loader.className = 'hidden';
document.body.appendChild(ajax_loader);
var prevpost_element = _$('prev_page_loader');
var pager_working = false;
var is_ie = false;
var scrollTop = 0;
var disable_autopage = false;
if(document.all) is_ie = true;
function _$(obj){
	return document.getElementById(obj);
}
function load_next_page(){
	if(pager_working) return;
	if(next_page > max_page) return;
	_$('next_page_waiter').style.display='block';
	_$('next_page_link').style.display='none';
	ajax_loader.onload = function(){ load_next_page_callback(); };
	ajax_loader.src = page_temp + '&page=' + next_page + '&skip=1';
	pager_working = true;
}
function load_next_page_callback(){
	var content = '';
	_$('next_page_waiter').style.display='none';
	_$('next_page_link').style.display='block';
	try{
		if(document.frames && document.frames['ajax_loader'].document){
			content = document.frames['ajax_loader'].document.getElementById('posts').innerHTML;
		}else{
			content = ajax_loader.contentDocument.getElementById('posts').innerHTML;
		}
	}catch(e){
		alert('无法加载页面，请稍后重试！');
		pager_working = false;
		return;
	}
	var _element = document.createElement('div');
	_element.innerHTML = content;
	_$('next_page_loader').appendChild(_element);
	if(next_page >= max_page) _$('next_page_link').style.display='none';
	next_page++;
	pager_working = false;
}
function load_prev_page(){
	if(pager_working) return;
	if(prev_page <= 0) return;
	_$('prev_page_waiter').style.display='block';
	_$('prev_page_link').style.display='none';
	ajax_loader.onload = function(){ load_prev_page_callback(); };
	ajax_loader.src = page_temp + '&page=' + prev_page + '&skip=1';
}
function load_prev_page_callback(){
	var content = '';
	_$('prev_page_waiter').style.display='none';
	_$('prev_page_link').style.display='block';
	try{
		if(document.frames && document.frames['ajax_loader'].document){
			content = document.frames['ajax_loader'].document.getElementById('posts').innerHTML;
		}else{
			content = ajax_loader.contentDocument.getElementById('posts').innerHTML;
		}
	}catch(e){
		alert('无法加载页面，请稍后重试！');
		pager_working = false;
		return;
	}
	var _element = document.createElement('div');
	_element.innerHTML = content;
	_$('prev_page_loader').parentNode.insertBefore(_element, prevpost_element);
	prevpost_element = _element;
	if(prev_page <= 1) _$('prev_page_link').style.display='none';
	prev_page--;
	pager_working = false;
}

// 自动翻页
function autopager(){
	if(disable_autopage) return;
	if(next_page > max_page) return;
	if(pager_working) return;
	scrollTop = is_ie ? document.documentElement.scrollTop : document.body.scrollTop;
	if(document.body.scrollHeight - scrollTop < screen.availHeight + 100) load_next_page();
}
if (document.addEventListener) {
	document.addEventListener('scroll', autopager, false);
}else{
	document.attachEvent('onscroll', autopager);
}